##
## FIGURE - CLUSTERING OF REGIONS -------------------------------- 
##

rm(list=ls())

###
### PACKAGES -----------------------------------------------------
### 

library(tidyverse)
library(ggpubr)
library(cluster)    # clustering algorithms
library(factoextra) # clustering algorithms & visualization
library(psych)
library(countrycode)
library(stringr)

theme <- theme_bw() + 
  theme(plot.title = element_text(hjust = 0),
        axis.title.x=element_text(hjust = 1.0, size=11),
        axis.title.y=element_text(size=11),
        panel.grid=element_blank())

##
## LOADING DATA ----------------------------------------------------------
##

load(file="Data for inequality assessment_regional level.RData")

ggplot()+
  geom_bar(data=aux1, 
           mapping=aes(x=achievement, 
                       y=fct_reorder(dimension, -order),
                       fill=as.factor(rural)), 
           stat="identity", 
           position = position_dodge(width = 0.9),
           alpha=0.4,
           width=0.7)+
  geom_point(data=aux2, 
             mapping=aes(x=achievement, 
                          y=fct_reorder(dimension, -order)),
             color="black",
             shape=124,
             size=5,
             vjust=0.1)+
  geom_text(data=aux2,
            mapping=aes(x=achievement,
                        y=fct_reorder(dimension, -order), 
                        label=paste(round(100*achievement,0),"%", sep="")),
            color="black",
            hjust=-0.3,
            size=3.2,
            alpha=0.7)+
  geom_hline(data=aux1, mapping=aes(yintercept=10.5), linetype = "dashed", color="grey")+
  facet_grid(rows=vars(education_highstatus_dum), cols=vars(occupation_agriculture_dum))+
  scale_fill_manual(name="",
                     values=c( "#0038e0", "#e01409"),
                     labels=c( "Urban", "Rural"))+
  ylab("")+xlab("% population with access to living standard")+
  scale_x_continuous(labels=scales::percent)+
  theme_bw()+
  theme(strip.background =element_rect(fill="white"),
        strip.text = element_text(colour = 'black', face='bold', size = 14),
        legend.position="bottom",
        axis.text=element_text(size=12),
        axis.title = element_text(size=14))

ggsave(filename="figure 3.png", width=12, height=8)
ggsave(filename="figure 3.svg", width=12, height=8)

  


